const api = require('./api.js');

/**
 * 发起网络请求
 * @param {Object} e 
 * @param {String} e.url 请求路径
 * @param {String} e.method 请求方式
 * @param {Object} e.data 请求体
 */
const request = (url,data,method = 'POST') => new Promise((t,f) => {
    const uri = 'https://bishe.my2171.com';
    // const uri = 'http://172.20.10.2:8081';
    wx.request({
        url: uri + url,
        data,method,
        header: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        success:(res) => {
            t(res)
        }
    })
})

// 获取 open_id
const getOpenId = () => new Promise((t,f) => {
    api.getStorage('userInfo').then(info => {
        if (info) t(info.open_id);
        else {
            wx.login({
                success(e) {
                    request('/student/getOpenId',{ code: e.code }).then(res => {
                        t(res.data.openid);
                    })
                },
            })
        }
    })
})

// 获取用户信息
const getUserInfo = () => new Promise((t,f) => {
    getOpenId().then(open_id => {
        request('/student/getInfo',{ open_id }).then(res => {
            api.hide();
            let { code,msg,data } = res.data;
            switch (code) {
                case 200:
                    t(data);
                    api.setStorage('userInfo',data);
                    break;
                case 501: t(null); break;
                default: api.showToast(msg,'none');
            }
        })
    })
})

// 获取系部列表
const getSdeptList = () => new Promise((t,f) => {
    request('/student/getSdepts').then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 获取班级列表
 * @param {*} sdept_id 系部 id
 */
const getClassBySdept = (sdept_id) => new Promise((t,f) => {
    request('/student/getClassBySdept', { sdept_id }).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

// 用户注册
const register = (e) => new Promise((t,f) => {
    request('/student/register',e).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200:
                api.hide();
                api.showToast('注册成功','success');
                setTimeout(() => {
                    api.navigateBack();
                }, 1500);
                break;
            default: api.showToast(msg,'none');
        }
    })
})

// 获取课题分类
const getTypes = () => new Promise((t,f) => {
    request('/topic/getTypes').then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

// 获取课题列表
const getTopic = (e) => new Promise((t,f) => {
    request('/topic/getList', e).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 搜索课题
 * @param {*} title 关键字 
 */
const searchTopic = (title) => new Promise((t,f) => {
    request('/topic/search',{ title }).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 获取课题详情
 * @param {*} id 课题 id
 */
const getTopicDetails = (id) => new Promise((t,f) => {
    request('/topic/getDetails',{ id }).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 获取小组信息
 * @param {*} id 小组 id
 */
const getTeamInfo = (id) => new Promise((t,f) => {
    request('/team/getInfo',{ id }).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 获取报名课题权限
 * @param {*} open_id open_id
 */
const getSignRoot = (open_id) => new Promise((t,f) => {
    getUserInfo(open_id).then(res => {
        if (res.team_id) {
            getTeamInfo(res.team_id).then(team => {
                if (team.topic_state === 0) t({ code: 500, msg: '小组已报名其他课题' });
                else if (team.leader_id === open_id) t({ code: 200, msg: 'ok' });
                else t({ code: 501, msg: '请联系组长完成报名' });
            })
        } else t({ code: 502, msg: '请加入小组后报名课题' });
    },ref => t({ code: 503, msg: '服务异常' }));
})

/**
 * 课题报名
 * @param {*} e { 用户 open_id, 课题 id } 
 */
const sign = (e) => new Promise((t,f) => {
    request('/team/sign',e).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 移除小组成员
 * @param {*} team_id 小组 id
 * @param {*} open_id 成员 id
 */
const delMember = (team_id,open_id) => new Promise((t,f) => {
    request('/team/delMember',{ team_id,open_id }).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 解散小组
 * @param {*} id 小组 id
 * @param {*} open_id 组长 id
 */
const dissol = (id,open_id) => new Promise((t,f) => {
    request('/team/dissol',{ id,open_id }).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 修改小组信息
 * @param {*} e 小组信息
 */
const editTeamInfo = (e) => new Promise((t,f) => {
    request('/team/editInfo',e).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 创建小组
 * @param {*} e 小组信息
 */
const createTeam = (e) => new Promise((t,f) => {
    request('/team/create',e).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 加入小组
 * @param {*} e 小组信息
 */
const joinTeam = (e) => new Promise((t,f) => {
    request('/team/join',e).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 获取公告列表
 * @param {*} e 参数
 */
const getNoticeList = (e) => new Promise((t,f) => {
    request('/notice/getStudentNotice',e).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 获取公告详情
 * @param {*} id 公告 id
 */
const getNoticeDetails = (id) => new Promise((t,f) => {
    request('/notice/getNoticeDetails',{ id }).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 修改头像
 * @param {*} e open_id avatar
 */
const editAvatar = (e) => new Promise((t,f) => {
    request('/student/editAvatar',e).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 修改姓名
 * @param {*} e open_id nickname
 */
const editNickname = (e) => new Promise((t,f) => {
    request('/student/editNickname',e).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 上传文件
 * @param {*} filePath 文件 url
 * @param {*} type 文件类型 0-图片 1-文档
 */
const upFile = (filePath,type=0) => new Promise((t,f) => {
    wx.uploadFile({
        url: 'https://upload.my2171.com/file/up',
        name: 'file',
        filePath,
        formData: {
            type,
            itemName: 'bishe'
        },
        success: (res) => {
            let { code,msg,data } = JSON.parse(res.data);
            switch (code) {
                case 200: t(data); break;
                default: api.showToast(msg,'none');
            }
        },
    })  
})

/**
 * 获取我的周报
 * @param {*} e open_id page pagesize
 */
const getMyReport = (e) => new Promise((t,f) => {
    request('/report/getMyReport',e).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 获取周报详情
 * @param {*} id 周报 id
 */
const getReportDetails = (id) => new Promise((t,f) => {
    request('/report/getDetails',{ id }).then(res => {
        let { code,msg,data } = res.data;
        switch (code) {
            case 200: t(data); break;
            default: api.showToast(msg,'none');
        }
    })
})

/**
 * 发布周报
 * @param {*} e 见接口文档
 */
const addReport = (e) => new Promise((t,f) => {
    request('/report/add',e).then(res => {
        let { code,msg } = res.data;
        switch (code) {
            case 200: t(); break;
            default: api.showToast(msg,'none');
        }
    })
})

export {
    request,            // 发起网络请求
    getOpenId,          // 获取 open_id
    getUserInfo,        // 获取用户信息（登录）
    getSdeptList,       // 获取系部列表
    getClassBySdept,    // 获取班级列表
    register,           // 用户注册
    getTypes,           // 获取课题分类
    getTopic,           // 获取课题列表
    searchTopic,        // 搜索课题
    getTopicDetails,    // 获取课题详情
    getTeamInfo,        // 获取小组信息
    getSignRoot,        // 获取报名课题权限
    sign,               // 课题报名
    delMember,          // 移除小组成员
    dissol,             // 解散小组
    editTeamInfo,       // 修改小组信息
    createTeam,         // 创建小组
    joinTeam,           // 加入小组
    getNoticeList,      // 获取公告列表
    getNoticeDetails,   // 获取公告详情
    editAvatar,         // 修改头像
    editNickname,       // 修改姓名
    upFile,             // 上传文件
    getMyReport,        // 获取我的周报
    getReportDetails,   // 获取周报详情
    addReport,          // 发布周报
}